// DIY模块地址: https://shop64022682.taobao.com/
#include "esp_camera.h"
#include <WiFi.h>
#include "driver/mcpwm.h"



#include "cam4FootsPins.h"
#include "robertStep.h"

#define USE_WIFI_MODE_AP // 屏蔽此行，WIFI设置为客户端模式，打开此行，WIFI设置为AP模式

#ifdef USE_WIFI_MODE_AP
const char *ssid = "wifiCam4Foots"; // AP模式的WIFI热点名称
const char *password = "12345678";// WIFI密码
#else
const char *ssid = "myWifi"; // 客户端模式下所要连接的WIFI热点名称，如家里使用的无线路由器WIFI热点名称
const char *password = "12345678"; // 客户端模式下所要连接的WIFI密码，如家里使用的无线路由器WIFI密码
#endif

void cam4FootsWebServerInit();
char *getCmdStr();

void setup()
{
  sg90_init();
  // 设置串口，用于打印日志
  Serial.begin(115200);
  Serial.setDebugOutput(true);

  camera_config_t camConfig;
  camConfig.pin_d0 = Y2_GPIO_NUM;
  camConfig.pin_d1 = Y3_GPIO_NUM;
  camConfig.pin_d2 = Y4_GPIO_NUM;
  camConfig.pin_d3 = Y5_GPIO_NUM;
  camConfig.pin_d4 = Y6_GPIO_NUM;
  camConfig.pin_d5 = Y7_GPIO_NUM;
  camConfig.pin_d6 = Y8_GPIO_NUM;
  camConfig.pin_d7 = Y9_GPIO_NUM;
  camConfig.pin_xclk = XCLK_GPIO_NUM;
  camConfig.pin_pclk = PCLK_GPIO_NUM;
  camConfig.pin_vsync = VSYNC_GPIO_NUM;
  camConfig.pin_href = HREF_GPIO_NUM;
  camConfig.pin_sscb_sda = SIOD_GPIO_NUM;
  camConfig.pin_sscb_scl = SIOC_GPIO_NUM;
  camConfig.pin_pwdn = PWDN_GPIO_NUM;
  camConfig.pin_reset = RESET_GPIO_NUM;
  camConfig.xclk_freq_hz = 15000000;
  camConfig.frame_size = FRAMESIZE_UXGA;
  camConfig.pixel_format = PIXFORMAT_JPEG; // for streaming
  camConfig.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
  camConfig.fb_location = CAMERA_FB_IN_PSRAM;
  camConfig.jpeg_quality = 12;
  camConfig.fb_count = 1;
  
  if(psramFound()){
    camConfig.jpeg_quality = 10;
    camConfig.fb_count = 2;
    camConfig.grab_mode = CAMERA_GRAB_LATEST;
  } else {
    // Limit the frame size when PSRAM is not available
    camConfig.frame_size = FRAMESIZE_SVGA;
    camConfig.fb_location = CAMERA_FB_IN_DRAM;
  }

  // 初始化摄像头
  esp_err_t err = esp_camera_init(&camConfig);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

  sensor_t *camSensor = esp_camera_sensor_get();
  camSensor->set_vflip(camSensor, 0); // flip it back 垂直翻转
  camSensor->set_hmirror(camSensor, 1); // flip it back 水平镜像
  camSensor->set_brightness(camSensor, 3); // 亮度设置
  camSensor->set_saturation(camSensor, 0); // 饱和度设置
#ifdef USE_WIFI_MODE_AP
  // 初始化AP模式下WIFI参数
  // 设置四足机器人的IP地址为192.168.0.1，对应浏览器中输入的四足机器人网址192.168.0.1
  // WiFi.softAPConfig(IPAddress("192.168.0.1"), IPAddress("192.168.0.1"), IPAddress("255.255.255.0"));
  WiFi.softAPConfig(IPAddress(192, 168, 0, 1), IPAddress(192, 168, 0, 1), IPAddress(255,255,255,0));
  // 设置WIFI热点名称,密码
  WiFi.softAP(ssid, password);
  // 通过串口打印WIFI参数
  Serial.println("WIFI SSID:");
  Serial.println(ssid);
  Serial.println("WIFI Password:");
  Serial.println(password);  
  Serial.print("Camera Ready! Use 'http://");
  Serial.print(WiFi.softAPIP());
  Serial.println("' to connect");
#else
  // 初始化客户端模式下WIFI参数
  // 设置所要连接的WIFI热点（无线路由）的名称、密码。
  WiFi.begin(ssid, password);
  WiFi.setSleep(false);
  // 等待WIFI热点（无线路由）连接成功
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // 输出连接成功信息
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("Camera Ready! Use 'http://");
  Serial.print(WiFi.localIP());
  Serial.println("' to connect");
#endif
  // 启动视频四足机器人WEB（网页）服务器，供浏览器登录使用
  cam4FootsWebServerInit();
}
void loop()
{
  runStepsCmd(getCmdStr());
  delay(100);
}
